package org.example.service.base;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author subframe7536
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ResponseResult<T> implements Serializable {

    public static final Long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 查询结果
     */
    private T data;

    private static <T> ResponseResult<T> restResult(T data, int code, String msg) {
        ResponseResult<T> apiResult = new ResponseResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public static <T> ResponseResult<T> ok() {
        return restResult(null, HttpEnum.OK_200.code(), HttpEnum.OK_200.info());
    }

    public static <T> ResponseResult<T> ok(T data) {
        return restResult(data, HttpEnum.OK_200.code(), HttpEnum.OK_200.info());
    }

    public static <T> ResponseResult<T> ok(T data, String msg) {
        return restResult(data, HttpEnum.OK_200.code(), msg);
    }

    public static <T> ResponseResult<T> failed() {
        return restResult(null, HttpEnum.ERROR_500.code(),
            HttpEnum.ERROR_500.info());
    }

    public static <T> ResponseResult<T> failed(String msg) {
        return restResult(null, HttpEnum.ERROR_500.code(), msg);
    }

    public static <T> ResponseResult<T> failed(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> ResponseResult<T> failed(T data) {
        return restResult(data, HttpEnum.ERROR_500.code(),
            HttpEnum.ERROR_500.info());
    }

    public static <T> ResponseResult<T> failed(T data, String msg) {
        return restResult(data, HttpEnum.ERROR_500.code(), msg);
    }


}

enum HttpEnum {

    /**
     * 请求处理正常
     */
    OK_200(200, "请求成功"),
    /**
     * 请求处理正常
     */
    CREATED_201(201, "创建成功"),
    /**
     * 请求处理异常，请稍后再试
     */
    ERROR_400(400, "非法请求"),
    /**
     * 访问内容不存在
     */
    NotFound_400(404, "访问内容不存在"),
    /**
     * 系统内部错误
     */
    ERROR_500(500, "系统内部错误"),
    /**
     * 参数校验失败
     */
    ERROR_600(600, "参数校验失败");

    private final String info;

    private final int code;

    HttpEnum(int code, String info) {
        this.info = info;
        this.code = code;
    }

    public String info() {
        return info;
    }

    public int code() {
        return code;
    }
}
